import { request } from "../utils"
import { Toast } from "vant"

/*
  获取用户优惠券
*/
const getUserCoupon = (requestData, cb) => {
  request({
    url: "api/Coupon/GetUserCounponList",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data.Coupon
      cb && cb(list)
    } else {
      console.log("获取用户优惠券失败...")
    }
  })
}

/*
  获取店铺优惠券
*/
const getShopCoupon = (requestData, cb) => {
  request({
    url: "api/Coupon/GetShopCouponList",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data.map(item => {
        item.StartTime = item.StartTime.split(' ')[0]
        item.EndTime = item.EndTime.split(' ')[0]
        return item
      })
      cb && cb(list)
    } else {
      console.log("获取店铺优惠券失败...")
    }
  })
}

/*
  用户获取店铺优惠券
*/
const userGetShopCoupon = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Coupon/PostAcceptCoupon",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("用户获取店铺优惠券失败...")
      Toast(res.data.Msg)
    }
  })
}

export { getUserCoupon, getShopCoupon, userGetShopCoupon }
